package idv.loveloser.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * MdlContext entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see idv.loveloser.dao.MdlContext
 * @author MyEclipse Persistence Tools
 */

public class MdlContextDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(MdlContextDAO.class);
	// property constants
	public static final String CONTEXTLEVEL = "contextlevel";
	public static final String INSTANCEID = "instanceid";
	public static final String PATH = "path";
	public static final String DEPTH = "depth";

	public void save(MdlContext transientInstance) {
		Transaction trx = getSession().beginTransaction();
		log.debug("saving MdlContext instance");
		try {
			getSession().save(transientInstance);
			trx.commit();
			trx=null;
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			trx=null;
			throw re;
		}
	}

	public void delete(MdlContext persistentInstance) {
		log.debug("deleting MdlContext instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MdlContext findById(java.lang.Long id) {
		log.debug("getting MdlContext instance with id: " + id);
		try {
			MdlContext instance = (MdlContext) getSession().get(
					"idv.loveloser.dao.MdlContext", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(MdlContext instance) {
		log.debug("finding MdlContext instance by example");
		try {
			List results = getSession()
					.createCriteria("idv.loveloser.dao.MdlContext")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding MdlContext instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from MdlContext as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByContextlevel(Object contextlevel) {
		return findByProperty(CONTEXTLEVEL, contextlevel);
	}

	public List findByInstanceid(Object instanceid) {
		return findByProperty(INSTANCEID, instanceid);
	}

	public List findByPath(Object path) {
		return findByProperty(PATH, path);
	}

	public List findByDepth(Object depth) {
		return findByProperty(DEPTH, depth);
	}

	public List findAll() {
		log.debug("finding all MdlContext instances");
		try {
			String queryString = "from MdlContext";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public MdlContext merge(MdlContext detachedInstance) {
		log.debug("merging MdlContext instance");
		Transaction trx =getSession().beginTransaction();
		try {
			MdlContext result = (MdlContext) getSession().merge(
					detachedInstance);
			trx.commit();
			trx=null;
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			trx=null;
			throw re;
		}
		
	}

	public void attachDirty(MdlContext instance) {
		log.debug("attaching dirty MdlContext instance");
		Transaction trx =getSession().beginTransaction();
		try {
			getSession().saveOrUpdate(instance);
			trx.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			trx=null;
			throw re;
		}
	}

	public void attachClean(MdlContext instance) {
		log.debug("attaching clean MdlContext instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}